/**
 * 
 */
package au.edu.swin.videoshop.posapp.ui.sections;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.component.UIData;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import au.edu.swin.videoshop.bean.Staff;
import au.edu.swin.videoshop.bean.StaffRole;
import au.edu.swin.videoshop.bean.Store;
import au.edu.swin.videoshop.service.StaffService;
import au.edu.swin.videoshop.service.StoreService;

/**
 * @author julie
 *
 */
public class StaffSection {
	private Staff mSelectedStaff;
	private List mSelectedStaffAvailableRoles;
	private List mSelectedStaffAvailableManagers;
	private List mSelectedStaffAvailableStores;
	
	private String mMode;
	private List mStaffs;
	
	@EJB
	private StaffService mStaffService;
	
	@EJB
	private StoreService mStoreService;
	
	public StaffSection() {
	}

	/**
	 * @return the selectedStaffRole
	 */
	public String getSelectedStaffRole() {
		if (mSelectedStaff.getRole() != null) {
			return mSelectedStaff.getRole().getName();
		}
		else {
			return null;
		}
	}

	/**
	 * @param selectedStaffRole the selectedStaffRole to set
	 */
	public void setSelectedStaffRole(String selectedStaffRole) {
		mSelectedStaff.setRole(StaffRole.getInstance(selectedStaffRole));
	}

	/**
	 * @return the selectedStaffSupervisorId
	 */
	public Long getSelectedStaffSupervisorId() {
		if (mSelectedStaff.getSupervisor() != null) {
			return mSelectedStaff.getSupervisor().getId();
		}
		return null;
	}

	/**
	 * @param selectedStaffSupervisorId the selectedStaffSupervisorId to set
	 */
	public void setSelectedStaffSupervisorId(Long selectedStaffSupervisorId) {
		mSelectedStaff.setSupervisor(mStaffService.findStaffByStaffId(selectedStaffSupervisorId));
	}

	/**
	 * @return the selectedStaffStoreId
	 */
	public Long getSelectedStaffStoreId() {
		if (mSelectedStaff.getStore() != null) {
			return mSelectedStaff.getStore().getStoreId();
		}
		return null;
	}

	/**
	 * @param selectedStaffStoreId the selectedStaffStoreId to set
	 */
	public void setSelectedStaffStoreId(Long selectedStaffStoreId) {
		mSelectedStaff.setStore(mStoreService.findStoreByStoreId(selectedStaffStoreId));
	}

	/**
	 * @return the selectedStaffAvailableStores
	 */
	public List getSelectedStaffAvailableStores() {
		return mSelectedStaffAvailableStores;
	}

	/**
	 * @return the selectedStaff
	 */
	public Staff getSelectedStaff() {
		return mSelectedStaff;
	}

	/**
	 * @return the selectedStaffAvailableRoles
	 */
	public List getSelectedStaffAvailableRoles() {
		if (mSelectedStaffAvailableRoles == null) {
			mSelectedStaffAvailableRoles = new ArrayList();
			
			FacesContext facesContext = FacesContext.getCurrentInstance();
			UserSection userSection = (UserSection)facesContext.getApplication().evaluateExpressionGet(facesContext, "#{userSection}", UserSection.class);

			if (userSection.getStaff().isManager()) {
				mSelectedStaffAvailableRoles.add(new SelectItem(StaffRole.CLERK.getName(), StaffRole.CLERK.getDescription()));
				mSelectedStaffAvailableRoles.add(new SelectItem(StaffRole.STORE_MANAGER.getName(), StaffRole.STORE_MANAGER.getDescription()));
			}
			if (userSection.getStaff().isSeniorManager()) {
				mSelectedStaffAvailableRoles.add(new SelectItem(StaffRole.SENIOR_MANAGER.getName(), StaffRole.SENIOR_MANAGER.getDescription()));
			}
		}
		return mSelectedStaffAvailableRoles;
	}

	/**
	 * @return the selectedStaffAvailableManagers
	 */
	public List getSelectedStaffAvailableManagers() {
		return mSelectedStaffAvailableManagers;
	}

	/**
	 * @return the staffs
	 */
	public List getStaffs() {
		if (mStaffs == null) {
			refreshStaffs();
		}
		return mStaffs;
	}

	public String add() {
		mSelectedStaff = new Staff();
		
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UserSection userSection = (UserSection)facesContext.getApplication().evaluateExpressionGet(facesContext, "#{userSection}", UserSection.class);

		if (userSection.getStaff().isManager()) {
			mSelectedStaff.setStore(userSection.getStaff().getStore());
		}
		
		mMode = "add";
		return "add";
	}
	
	public String edit() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UIViewRoot root = facesContext.getViewRoot();
		UIData table = (UIData)root.findComponent("staffForm").findComponent("staffTable");
		mSelectedStaff = (Staff) table.getRowData();
		mMode = "edit";
		
		return "edit";
	}
	
	public String save() {
		if (mSelectedStaff != null) {
			if (mMode.equals("add")) {
				mStaffService.addStaff(mSelectedStaff);
			}
			else if (mMode.equals("edit")) {
				mStaffService.updateStaff(mSelectedStaff);
			}
			
			refreshStaffs();
		}
		
		return "browse";
	}
	
	public String delete() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UIViewRoot root = facesContext.getViewRoot();
		UIData table = (UIData)root.findComponent("staffForm").findComponent("staffTable");
		Staff staff = (Staff) table.getRowData();
		mStaffService.markDeleteStaff(staff.getStaffId());
		
		refreshStaffs();
		
		return null;
	}
	
	private void refreshStaffs() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UserSection userSection = (UserSection)facesContext.getApplication().evaluateExpressionGet(facesContext, "#{userSection}", UserSection.class);

		if (userSection.getStaff().isSeniorManager()) {
			mStaffs = mStaffService.findAllStaffs();
			List stores = mStoreService.findAllStores();
			mSelectedStaffAvailableStores = new ArrayList();
			for (Iterator i = stores.iterator(); i.hasNext();) {
				Store store = (Store) i.next();
				mSelectedStaffAvailableStores.add(new SelectItem(store.getStoreId(), store.getStoreName()));
			}
		}
		else if (userSection.getStaff().isManager()) {
			Store store = userSection.getStaff().getStore();
			
			mStaffs = mStaffService.findStaffsByStoreId(store.getStoreId());
			
			mSelectedStaffAvailableStores = new ArrayList();
			mSelectedStaffAvailableStores.add(new SelectItem(store.getStoreId(), store.getStoreName()));
		}

		mSelectedStaffAvailableManagers = new ArrayList();
		for (Iterator i = mStaffs.iterator(); i.hasNext();) {
			Staff staff = (Staff) i.next();
			
			if (staff.isManager()) {
				mSelectedStaffAvailableManagers.add(new SelectItem(staff.getStaffId(), staff.getFirstName() + " " + staff.getLastName()));
			}
		}
		
	}
}
